#include "CSprite.h"
#include <d3dx9.h>
#include <d3dx9math.h>

CSprite::CSprite()
{
	m_FilePath		= NULL;
	m_SpriteHandler	= NULL;
	m_Image			= NULL;

	m_TransColor	= NULL;

	mi_Count		= 0;
	mi_PerRow		= 0;
	
	mi_Width		= 0;
	mi_Height		= 0;
	mi_Index		= 0;
	mi_SpriteLine	= 0;

	mf_Scale		= 0;
}

CSprite::~CSprite(void)
{
	if (m_SpriteHandler != NULL)
	{
		m_SpriteHandler = NULL;
	}

	if (m_Image != NULL)
	{
		m_Image = NULL;
	}
}

void CSprite::CreateSprite(LPD3DXSPRITE SpriteHandler, LPCSTR FilePath, int iWidth, int iHeight, int iPerRow, int iCount, 
							float fScale, D3DCOLOR TransColor)
{
	m_FilePath			= FilePath; 
	m_SpriteHandler		= SpriteHandler;
	m_Image				= NULL;

	m_TransColor = TransColor;

	mi_Count	= iCount;
	mi_PerRow	= iPerRow;
	
	mi_Width	= iWidth;
	mi_Height	= iHeight;
	mi_Index	= 0;	
	mi_SpriteLine = 0;

	mf_Scale = fScale;	

	LoadImage();
}

void CSprite::LoadImage()
{
	HRESULT result;
	
	D3DXIMAGE_INFO Image_Info;

	result = D3DXGetImageInfoFromFile(m_FilePath, &Image_Info);
	
	LPDIRECT3DDEVICE9 d3ddv; 
	m_SpriteHandler->GetDevice(&d3ddv);

	result = D3DXCreateTextureFromFileEx(	d3ddv,
											m_FilePath,			//image file path
											Image_Info.Width,	//image's width
											Image_Info.Height,	//image's height
											1,					//mip-map levels (1 for no chain)
											D3DPOOL_DEFAULT,	//the type of the surface (standar) 
											D3DFMT_UNKNOWN,		//surface format (default)
											D3DPOOL_DEFAULT,	//memory class for the texture
											D3DX_DEFAULT,		//image filter
											D3DX_DEFAULT,		//mip filter
											m_TransColor,		//color key for transparency
											&Image_Info,		//bitmap file info (from loaded file)
											NULL,				//color palette
											&m_Image);			//destination texture
}

//render and scaling
//
void CSprite::Render(int ix, int iy)
{
	//the tile that we take out from the source image
	RECT srcRect;

	srcRect.top		= mi_SpriteLine*mi_Height;
	srcRect.bottom	= srcRect.top + mi_Height;
	srcRect.left	= mi_Index*mi_Width;
	srcRect.right	= srcRect.left + mi_Width;
	
	//calculate the center of the sprite to scaling
	float fx_center = (float) ix + (float) mi_Width/2;
	float fy_center = (float) iy + (float) mi_Height/2;
			
	//scaling the image with the ScaleRate
	Transform(fx_center, fy_center);

	//position of the sprite
	D3DXVECTOR3 Position(ix, iy, 0);

	//draw
	m_SpriteHandler->Draw(	m_Image,	//source image
							&srcRect,	//source rectange
							NULL,		
							&Position,	
							0xFFFFFFFF);
}

void CSprite::Transform(float fx_center, float fy_center)
{

	D3DXVECTOR2 SpriteCenter(fx_center,fy_center);

	D3DXVECTOR2 Trans(mi_Width*(mf_Scale-1)/2, mi_Height*(mf_Scale-1)/2);
	//D3DXVECTOR2 Trans(0, 0);

	D3DXVECTOR2 Scaling(mf_Scale, mf_Scale);

	//rotate based on the time passed
	//float rotation = GetTickCount()/1000.0f;

	//build our matrix to rotate, scale and position our sprite
	D3DXMATRIX Matrix;

	D3DXMatrixTransformation2D(	&Matrix,	//out
								&SpriteCenter,	//scaling center
								0.0,		//scaling rotation
								&Scaling,	//scaling
								&SpriteCenter, //rotation center
								D3DXToRadian(0),	//rotation
								&Trans);	//translation 

	m_SpriteHandler->SetTransform(&Matrix);
}

//set methods
//
void CSprite::SetSpriteLine(int iLine)
{
	mi_SpriteLine = iLine;
}
void CSprite::SetIndex(int iIndex)
{
	mi_Index = iIndex;
}

//other methods
//
void CSprite::Next(void)
{
	mi_Index = ((mi_Index +1) % mi_PerRow);
}

void CSprite::Reset(void)
{
	mi_Index = 0;
}

//get methods
//

int CSprite::GetSpriteLine(void)
{
	return mi_SpriteLine;
}

int CSprite::GetPerRow(void)
{
	return mi_PerRow;
}

int CSprite::GetCount(void)
{
	return mi_Count;
}

int CSprite::GetWidth(void)
{
	return mi_Width;
}

int CSprite::GetHeight(void)
{
	return mi_Height;
}

int CSprite::GetIndex(void)
{
	return mi_Index;
}

